#/bin/python3
#coding=utf8
# import requests,zlib,pymysql,traceback,cchardet
# import base64,os,json
# from lxml import etree

# from urllib.parse import urlparse

# url = "https://mil.news.sina.com.cn/dgby/"

# mysql_conent = {}
# mysql_conent['host']='127.0.0.1'
# mysql_conent['user']='root'
# mysql_conent['passwd']=''
# mysql_conent['database']='test'
# mysql_conent['port']='3306'


# def conn_mysql_action_data(conn,sql_str):
#     conn = pymysql.connect(mysql_conent)
#     try :
#         cursor = conn.cursor()
#         cursor.execute(sql_str)

#     except :
#         traceback.print_exc()

#     conn.close()



# #请求url 获得网站响应

# # res = requests.get(url)


# # # body = res.text.encode('utf8')
# # encoding = cchardet.detect(res.content)['encoding']
# # body = res.content.decode(encoding)

# # html = pymysql.escape_string(body)
# # body = '福建省看到了房价'.encode('utf8')
# # print(body)
# # print(body.decode('utf8'))
# # # os.exit()

# # # print(type(body))

# # # html = base64.b64encode(body)

# # html = base64.encodebytes(body)
# # print('htmlhtmlhtml')
# # print(html)
# # print('htmlhtmlhtml')
# # last_body = base64.decodebytes(html)
# # print('last_body')
# # print(last_body.decode('utf8'))
# # print('last_body')


# # print(type(body))
# # exit()
# # html = zlib.compress(body.encode('utf8'))


# #------------------- 添加内容才要这里-----------------
# # res = requests.get(url)
# # encoding = cchardet.detect(res.content)['encoding']
# # body = res.content.decode(encoding)
# # html = pymysql.escape_string(body)
# # title = u'新浪新闻'
# #------------------- 添加内容才要这里-----------------

# # #sql_str = "INSERT INTO tb_url(`title`,`url`,`html`) VALUE ('%s','%s','%s'); " % (title.decode('utf8') , url , pymysql.escape_string(body)
# # # sql_str = "INSERT INTO tb_url(`title`,`url`,`html`) VALUE ('%s','%s','%s'); " % (title.decode('utf8') , url , html)


# try :
#     conn = pymysql.connect(host="127.0.0.1",user='root',passwd='',database='test',port=3306)
#     # conn = pymysql.connect(mysql_conent)
    
    
#     # f = open('sql.txt','w+')
#     # f.write(sql_str)
#     # f.close()
#     # sql_str = 'show create table pc_url;'

#     #插入数据的sql语句字段要一一对应，不然会报错，
#     # pymysql.escape_string 可以使用这个 来转义html内容
#     #请求过来的数据是 byte型数据，需要转化为string类型
    
#     # 插入sql的语句
    
    
#     #读取sql的语句
#     # sql_str ='show tables;'
#     # sql_str ='select * from tb_url where id= %d' % (1)
    
#     cursor = conn.cursor()
#     try:
#         # 执行sql语句
#         # 插入数据

#         #原来这个句子没有问题
#         # pymysql.escape_string(body) 转义html里面的字符串也没有问题 
#         ##### 需要数据和表的类型设置为 utf8mb4 这种类型
#         # sql_str = 'INSERT INTO tb_url(`title`,`url`,`html`) VALUE ("%s","%s","%s"); ' % ( title,url, html)

#         # 查看数据
#         sql_str = 'select * from tb_url where id= %d' % (2)

#         # last = cursor.execute(sql_str)
#         cursor.execute(sql_str)
#         data = cursor.fetchone()
#         print('last')
#         # print(data[3])
#         last_result = data[3]
#         result_html = etree.HTML(last_result)
#         # help(result_html)

#         a_list= result_html.xpath('//a')
#         for a_i in a_list:
#             # print(a_i.xpath('@href')[0],a_i.xpath('.//text()'))
            
#             print(a_i.values(),a_i.attrib.get('href'),a_i.text)
#             # print(a_i.text)
#             # print(json.dumps(a_i.__dict__))
#             # print(a_i.attrib.get('href'))
#             # print(help(a_i.attrib.get('href')))
#             # break
#         # print(a_list)

#         # print(data[1].decode('utf8'))
#         #base64 解码
#         # print(base64.b64decode(data[3]))


#         # print(type(base64.b64decode(data[3])))
        
#         # last_html = base64.decodebytes(data[3].encode('utf8'))

#         # print(type(last_html))

#         # bs_first = base64.decodebytes(data[3].encode())

#         # bs_firstencoding = cchardet.detect(bs_first)['encoding']

#         # # bs_firstbody = bs_first.decode(bs_firstencoding)

#         # print(bs_firstencoding)
#         # print(str(bs_first, 'utf-8'))
#         # print(bs_first.decode('gbk').encode('utf8'))
#         # print(type(bs_firstbody))
#         # print(type(data[3]))

#         # print(base64.b64decode(data[3]).decode('utf8'))
#         # print(zlib.decompress(bytes(data[3], encoding = "utf8")))
#         #print(zlib.decompress(data[3]))
#         print('last')
        
#         # 提交到数据库执行
#         conn.commit()
#     except:
#     # 如果发生错误则回滚
#         conn.rollback()
#         traceback.print_exc()
#     conn.close()
    
# except :
#     traceback.print_exc()



# import requests,zlib,pymysql,traceback,cchardet
# import base64,os,json
# from lxml import etree

# from urllib.parse import urlparse

# url = "https://mil.news.sina.com.cn/dgby/"

# result = urlparse(url)
# print(result)

# 线程池和进城池
from concurrent.futures import ProcessPoolExecutor  # 进程池模块
from concurrent.futures import ThreadPoolExecutor  # 线程池模块
import os, time, random

#  下面是以进程池为例， 线程池只是模块改一下即可
# def talk(name):
#     print('name: %s  pis%s  run' % (name,os.getpid()))
#     time.sleep(random.randint(1, 3))

# if __name__ == '__main__':
#     pool = ProcessPoolExecutor(4)  # 设置线程池大小，默认等于cpu核数
#     for i in range(10):
#         pool.submit(talk, '进程%s' % i)  # 异步提交（只是提交需要运行的线程不等待）

#     # 作用1：关闭进程池入口不能再提交了   作用2：相当于jion 等待进程池全部运行完毕
#     pool.shutdown(wait=True)  
#     print('主进程')

#线程池
# def talk(name):
    # print('name: %s  pis%s  run' % (name,os.getpid()))
    # time.sleep(random.randint(1, 3))

# if __name__ == '__main__':
    # pool = ThreadPoolExecutor(20)  # 设置线程池大小，默认等于cpu核数
    # for i in range(1000):
        # pool.submit(talk, '线程%s' % i)  # 异步提交（只是提交需要运行的线程不等待）

    # 作用1：关闭进程池入口不能再提交了   作用2：相当于jion 等待进程池全部运行完毕
    # pool.shutdown(wait=True)  
    # print('主进程')
    

import requests,zlib,pymysql,traceback,cchardet,time,json



new_url = 'https://feed.sina.com.cn/api/roll/get?pageid=121&lid=1356&num=20&versionNumber=1.2.4&page=1&encode=utf-8&callback=feedCardJsonpCallback&_=1626418225448'
res = requests.get(new_url)

encoding = cchardet.detect(res.content)['encoding']
body = res.content.decode(encoding)

# print(type(body))

# print(body)
# print("\n\n")
# print("\n\n")

#输出的结果不一样 strip的删除 会多删除 { } 这种类型的字符串 建议多看看这个 strip方法 或者用replace 来替换就好
#replace 替换匹配的字符串

# jsonstr = body.lstrip('try{feedCardJsonpCallback(').rstrip(';}catch(e){};');

# print(jsonstr)
# print("\n\n")

jsonstr1 = body.replace("try{feedCardJsonpCallback(", "").replace(");}catch(e){};", "")

# print(jsonstr1)


jsonstrdata = json.loads(jsonstr1)
# print(jsonstrdata['result']['data'])


data = jsonstrdata['result']['data']

needdata = dict()
n=0
for i in data:
    # print(i)
    # print(i['title'])
    # print(i['url'])
    # print("\n\n\n\n")
    
    # needdata[n] = {url:i['url'].replace('\/','/'),title:i['title'].decode(encoding)}
    needdata[n] = {'url':i['url'],'title':i['title']}
    n=n+1
print(needdata)